package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.baseinfo.dao.SchoolcalendarDayDao;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarDay;
import com.orhon.smartcampus.modules.baseinfo.gqlInputModel.GQLSchoolCalendarDayInput;
import com.orhon.smartcampus.modules.baseinfo.inputMapper.SchoolCalendarDayInputMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EditSchoolCalendarDayMutation implements DataFetcher {


    @Autowired
    private SchoolcalendarDayDao schoolcalendarDayDao;

    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLSchoolCalendarDayInput input = SchoolCalendarDayInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        SchoolcalendarDay day = null;
        day = schoolcalendarDayDao.get(SchoolcalendarDay.class, input.getId());
        day.setUpdatedAt(new Date());
        day.setName(JSON.toJSONString(input.getName()));
        try {
            schoolcalendarDayDao.saveOrUpdate(day);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}
